package com.qst.system.controller;

import com.qst.common.core.controller.BaseController;
import com.qst.common.core.domain.AjaxResult;
import com.qst.common.core.page.TableDataInfo;
import com.qst.system.domain.vo.LoansVo;
import com.qst.system.service.ILoansService;
import org.apache.poi.ss.formula.functions.T;
import org.bouncycastle.cms.PasswordRecipientId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/system/loans")
public class LoansController extends BaseController {

    @Autowired
    private ILoansService loansService;
    @PreAuthorize("@ss.hasPermi('system:loans:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoansVo loansVo){
        startPage();
        List<LoansVo> list = loansService.selectLoansList(loansVo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:loans:add')")
    @PostMapping
    public AjaxResult add(LoansVo loansVo){
        return toAjax(loansService.insertLoans(loansVo));
    }

    @PreAuthorize("@ss.hasPermi('system:loans:edit')")
    @PutMapping
    public AjaxResult edit(LoansVo loansVo){
        System.out.println("==========================进入");
        System.out.println(loansVo.toString());
        return toAjax(loansService.updateLoans(loansVo));
    }

    @PreAuthorize("@ss.hasPermi('system:loans:remove')")
    @DeleteMapping("/{loaIds}")
    public AjaxResult remove(@PathVariable Long[] loaIds){
        if (loaIds.length == 1){
            return toAjax(loansService.deleteLoansById(loaIds[0]));
        }
        return toAjax(loansService.deleteLoansByIds(loaIds));
    }

    @PreAuthorize("@ss.hasPermi('system:loans:list')") // 与前端v-hasPermi权限一致
    @GetMapping("/loansNo")
    public AjaxResult getLoansNo() {
        // 调用Service获取贷款编号列表（返回格式：[{loaId: 1, loaNo: "LOAN001"}, ...]）
        List<Map<String, Object>> loansNoList = loansService.selectLoansNoList();
        return AjaxResult.success(loansNoList);
    }

    @GetMapping("/bankName")
    @PreAuthorize("@ss.hasPermi('system:loans:bank:list')")
    public AjaxResult getBankNames() {
        return AjaxResult.success(loansService.getBankNames());
    }

    @PreAuthorize("@ss.hasPermi('system:loans:list')")
    @GetMapping("/bankLoans")
    public TableDataInfo getBankLoans(LoansVo loansVo){
        startPage();
        List<LoansVo> loansVos = loansService.selectLoansList(loansVo);
        return getDataTable(loansVos);
    }

    @PreAuthorize("@ss.hasPermi('system:loans:edit')")
    @GetMapping("/{eId}")
    public AjaxResult getLoansById(@PathVariable Long eId){
        LoansVo loansVo = loansService.selectLoansById(eId);
        return AjaxResult.success(loansVo);
    }
    
    @PreAuthorize("@ss.hasPermi('system:loans:list')")
    @GetMapping("/loansById/{eId}")
    public TableDataInfo getLoansListById(@PathVariable Long eId){
        LoansVo loansVo = loansService.selectLoansById(eId);
        List<LoansVo> list = new ArrayList<>();
        if (loansVo != null) {
            list.add(loansVo);
        }
        return getDataTable(list);
    }
    
    @PreAuthorize("@ss.hasPermi('system:loans:list')")
    @GetMapping("/aptitudeById/{eId}")
    public TableDataInfo getAptitudeById(@PathVariable Long eId){
        LoansVo loansVo = loansService.selectLoansById(eId);
        List<LoansVo> list = new ArrayList<>();
        if (loansVo != null) {
            list.add(loansVo);
        }
        return getDataTable(list);
    }
    
    @PreAuthorize("@ss.hasPermi('system:loans:list')")
    @GetMapping("/materialsById/{eId}")
    public TableDataInfo getMaterialsById(@PathVariable Long eId){
        LoansVo loansVo = loansService.selectLoansById(eId);
        List<LoansVo> list = new ArrayList<>();
        if (loansVo != null) {
            list.add(loansVo);
        }
        return getDataTable(list);
    }

}